package com.zjp;

import cn.hutool.core.util.RandomUtil;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import com.zjp.domain.Apple;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.*;

public class OrderTest {


    public static List<Apple> appleList = new ArrayList<>();


    public static String getStr() {

        return RandomUtil.randomInt(1, 100) > 50 ? null : RandomUtil.randomString(10);
    }

    @BeforeAll
    public static void d1() {
        Apple a1 = new Apple();
        a1.setWeight(RandomUtil.randomInt(1, 100));
        a1.setColor(getStr());
        a1.setSort(RandomUtil.randomInt(1, 100));
        Apple a2 = new Apple();
        a2.setWeight(RandomUtil.randomInt(1, 100));
        a2.setColor(getStr());
        a2.setSort(RandomUtil.randomInt(1, 100));
        Apple a3 = new Apple();
        a3.setWeight(RandomUtil.randomInt(1, 100));
        a3.setColor(getStr());

        appleList.add(a1);
        appleList.add(a2);

        appleList.add(null);
        appleList.add(a3);

    }

    @Test
    public void r1() {
        // 自然排序
        String[] str = {"B", "E", "A"};

        Arrays.sort(str, Ordering
                .natural());
        System.out.println(Arrays.toString(str));
    }

    @Test
    public void r2() {
        // 空值优先
        String[] str = {"B", null, "E", "A"};

        Arrays.sort(str, Ordering
                .natural().nullsFirst());
        System.out.println(Arrays.toString(str));
    }

    @Test
    public void t1() {

        // natural() 对可排序类型做自然排序，如数字按大小，日期按先后排序


        String str1 = RandomUtil.randomString(8);
        String str2 = RandomUtil.randomString(10);


        Ordering<String> stringOrdering = new Ordering<String>() {
            @Override
            public int compare(String left, String right) {
                return Ints.compare(left.length(), right.length());
            }
        };
        int compare = stringOrdering.compare(str1, str2);

        toCompareResult(compare);

        toCompareResult(Comparator.<String>naturalOrder().compare(str1, str2));
    }

    @Test
    public void t2() {

        // 排序方式
        // 颜色为空
        // 重量从大到小


        Ordering<@Nullable Apple> ordering = new Ordering<Apple>() {

            @Override
            public int compare(Apple a1, Apple a2) {
                if (Objects.isNull(a1.getColor()) && Objects.isNull(a2.getColor())) {
                    return ComparisonChain.start()
                            .compare(a1.getWeight(), a2.getWeight(), Ordering.natural().reversed())
                            .result();
                }

                if (Objects.isNull(a1.getColor())) {
                    return -1;
                }
                if (Objects.isNull(a2.getColor())) {
                    return 1;
                }
                return ComparisonChain.start()
                        .compare(a1.getWeight(), a2.getWeight(), Ordering.natural().reversed())
                        .result();
            }
        }.nullsFirst();


        appleList.forEach(System.out::println);

        System.out.println("--------------------------");
        List<Apple> apples = ordering.sortedCopy(appleList);

        apples.forEach(System.out::println);

    }

    @Test
    public void t3() {
        Ordering<Apple> appleOrdering = Ordering.natural().nullsFirst().onResultOf(Apple::getSort);


        appleList.forEach(System.out::println);

        System.out.println("--------------------------");
        List<Apple> apples = appleOrdering.sortedCopy(appleList);

        apples.forEach(System.out::println);
    }

    private void toCompareResult(int compare) {
        if (compare < 0) {
            System.out.println("str2 大一点");
        } else if (compare == 0) {
            System.out.println("str1 str2 一样大");
        } else {
            System.out.println("str1 大一点");
        }
    }
}
